package demo8;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Iterator;
import java.util.Stack;

public class Hanoi_Tower {
    /**
     * 可以记住相同的逻辑 。 举一反三，
     */
    public void simpleMethod(){

    }
    @Data
    @NoArgsConstructor
    public class Tower{
        private String name;

        public Tower(String name) {
            this.name = name;
        }

        Stack<Integer> integers = new Stack<>();
        public void add(int item){
            // 1<0
            if(integers.size()>0 && integers.peek()<item){
                throw new RuntimeException("val is not correctly ");
            }
            integers.push(item);
        }
        private int peek(){
            return integers.peek();
        }
        private int pop(){
            return integers.pop();
        }
        private void push(int item){
             integers.push(item);
        }
        public void print(){
            Iterator<Integer> iterator = integers.iterator();
            while (iterator.hasNext()){
                System.out.print(iterator.next()+",");
            }
            System.out.println();
        }
        private void moveTopUpTo(Tower destination){
            int pop = this.pop();
            destination.push(pop);
        }
        public void move(int num,Tower buffer, Tower destination){
            if(num!=0){
                this.move(num-1,destination,buffer);
                System.out.println(this.peek()+" from "+this.name+" to "+ destination.name);
                this.moveTopUpTo(destination);
                buffer.move(num-1,this,destination);
            }

        }
    }

    public void moveInitAndStart(int size){
        Tower source = new Tower("source");
        Tower buffer =new Tower("buffer");
        Tower destination = new Tower("destination");
        for (int i = size; i >=1 ; i--) {
            source.add(i);
        }
        source.print();
        destination.print();
        source.move(size,buffer,destination );
        destination.print();

    }

}
